package me.zhengjie.business.rest;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.zhengjie.annotation.Log;
import me.zhengjie.business.domain.Hljg;
import me.zhengjie.business.domain.Zhsq;
import me.zhengjie.business.domain.sup.Gsgl;
import me.zhengjie.business.domain.vo.KC04;
import me.zhengjie.business.service.GsglService;
import me.zhengjie.business.service.Kc04Service;
import me.zhengjie.business.service.ZhsqService;
import me.zhengjie.business.service.dto.ZhsqDto;
import me.zhengjie.business.service.dto.ZhsqQueryCriteria;
import me.zhengjie.config.DebugProperties;
import me.zhengjie.utils.SecurityUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * @website https://el-admin.vip
 * @author xiahongbo
 * @date 2022-12-23
 **/
@Slf4j
@RestController
@RequiredArgsConstructor
@Api(tags = "终止待遇")
@RequestMapping("/api/stopZhsq")
public class ZhsqStopController {

    private final Kc04Service kc04Service;
    private final ZhsqService zhsqService;
    private final GsglService gsglService;

    @GetMapping(value = "/queryBySfz")
    public ResponseEntity<Object> queryBySfz(@RequestParam String sfz) {

        ZhsqDto zhsqDto = zhsqService.queryBySfz(sfz);

        zhsqDto = zhsqDto == null ? kc04Service.queryBySfz(sfz) : zhsqDto;

        log.info("zhsqDto: {} ", zhsqDto);

        return new ResponseEntity<>(zhsqDto, HttpStatus.OK);
    }

    @GetMapping(value = "/queryByGrbm")
    public ResponseEntity<Object> queryByGrbm(@RequestParam String grbm) {

        KC04 kc04 = kc04Service.findKc04(grbm);

        if(!StringUtils.isEmpty(kc04.getAkc318())) {
            Gsgl gsgl = gsglService.findByBisGsglYljgdm(kc04.getAkc318().toString());
            kc04.setAkc318Mc(gsgl.getBisGsglQymc());
        } else {
            kc04.setAkc318Mc("");
        }


        return new ResponseEntity<>(kc04, HttpStatus.OK);
    }

    @GetMapping(value = "/queryAllZhsq")
    public ResponseEntity<Object> queryAllZhsq(ZhsqQueryCriteria criteria, Pageable pageable) {

        Long userid = SecurityUtils.getCurrentUserId();
        criteria.setBisZhsqZhlb("01");
        UserDetails userDetails = SecurityUtils.getCurrentUser();

        JSONObject userJson = new JSONObject(new JSONObject(userDetails).get("user"));
        JSONArray array = JSONUtil.parseArray(userJson.get("roles"));
        String roleString = array.toString();
        if(roleString.indexOf("居家照护") != -1 || roleString.indexOf("机构照护") != -1 || roleString.indexOf("机构住院") != -1 || roleString.indexOf("居家机构双角色") != -1 || roleString.indexOf("机构住院批量") != -1) {
            log.info("角色：居家照护、机构照护、居家机构双角色");
            Gsgl gsgl = gsglService.queryGsgl(userid);
            Hljg hljg = new Hljg();
            hljg.setBisGsglId(gsgl.getBisGsglId());
            criteria.setHljg(hljg);
        } else if(roleString.indexOf("中心人员") != -1 || roleString.indexOf("中心人员1") != -1 || roleString.indexOf("管理员") != -1 || roleString.indexOf("超级管理员") != -1) {

        }
        List<String> list = new ArrayList<>();
        list.add("05");
        criteria.setBisZhsqZt(list);

        return new ResponseEntity<>(zhsqService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PutMapping
    @Log("机构执行终止待遇")
    @ApiOperation("机构执行终止待遇")
    public ResponseEntity<Object> stopZhsq(@RequestParam String sfz, @RequestParam String zzrq, @RequestParam Long zhid) {

        kc04Service.stopZhsq(sfz, zzrq, zhid, "死亡注销");
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }
}
